document.addEventListener('DOMContentLoaded', function() {
    const sourceText = document.getElementById('sourceText');
    const targetText = document.getElementById('targetText');
    const translateBtn = document.getElementById('translateBtn');
    const clearBtn = document.getElementById('clearBtn');
    const loading = document.getElementById('loading');
    const modelStatus = document.getElementById('modelStatus');
    const statusText = document.getElementById('statusText');

    // 模拟模型加载
    setTimeout(() => {
        modelStatus.classList.add('ready');
        statusText.textContent = '模型已就绪';
    }, 2000);

    translateBtn.addEventListener('click', function() {
        const text = sourceText.value.trim();
        if (text) {
            loading.style.display = 'inline-block';
            translateBtn.disabled = true;
            
            // 检查输入是否为英文
            const isEnglish = /^[a-zA-Z\s.,!?:;'"()-]+$/.test(text);
            if (!isEnglish) {
                loading.style.display = 'none';
                translateBtn.disabled = false;
                targetText.value = "请输入英文文本进行翻译";
                return;
            }

            // 发送消息给background script进行翻译
            chrome.runtime.sendMessage({action: "translateText", text: text}, function(response) {
                if (chrome.runtime.lastError) {
                    console.error('错误:', chrome.runtime.lastError);
                    targetText.value = '翻译失败，请检查翻译服务是否运行';
                } else {
                    targetText.value = response.translatedText;
                }
                loading.style.display = 'none';
                translateBtn.disabled = false;
            });
        }
    });

    clearBtn.addEventListener('click', function() {
        sourceText.value = '';
        targetText.value = '';
    });

    // 翻译整个网页按钮事件
    const translatePageBtn = document.getElementById('translatePageBtn');
    const pageLoading = document.getElementById('pageLoading');

    translatePageBtn.addEventListener('click', function() {
        pageLoading.style.display = 'inline-block';
        translatePageBtn.disabled = true;

        // 获取当前活动标签页
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            // 发送消息给当前标签页的content script
            chrome.tabs.sendMessage(tabs[0].id, {action: 'translatePage'}, function(response) {
                pageLoading.style.display = 'none';
                translatePageBtn.disabled = false;
                
                if (chrome.runtime.lastError) {
                    console.error('发送消息失败:', chrome.runtime.lastError);
                    alert('翻译网页失败，请刷新页面后重试');
                } else if (response && response.status === 'started') {
                    targetText.value = '✅ 网页翻译已开始，正在翻译页面内容...';
                    // 3秒后关闭弹出窗口，让用户查看翻译结果
                    setTimeout(() => {
                        window.close();
                    }, 2000);
                }
            });
        });
    });
});